﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoRegisterManager.Common;
using System.Threading;

namespace AutoRegisterManager
{
    public partial class FrmInCardUniversal : Form
    {
        MyAlert m = new MyAlert();

        public FrmInCardUniversal()
        {
            InitializeComponent();
        }

        private void FrmInCardUniversal_Load(object sender, EventArgs e)
        {
            FrmMain.patientInfoData = new DataSet();
            FrmMain.cardData = new DataSet();
            FrmMain.cardBlance = 0;
            if (this.top1.AutoClose)
            {
                this.top1.timer1.Start();
            }
            #region 读卡器读卡
            //此处写读卡器读卡代码
            timer1.Start();
            #endregion

            txtInputCard.Focus();
        }

        private void FrmInCardUniversal_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.top1.timer1.Stop();
            this.timer1.Stop();
        }

        private void closePort()
        {

            #region 关闭读卡器端口
            //axTTDriverMgr1.CREjectCard(0);
            //int rs = axTTDriverMgr1.CRCancelWait();
            //if (rs != 0)
            //{
            //    int errorNum = axTTDriverMgr1.GetDeviceStatus(TTDeviceMgr.DT_CARDREADER);
            //    MyALert.MsgInfo(TTDeviceMgr.GetStatusInfo(errorNum) + ",错误代码：" + errorNum.ToString());
            //    return;
            //}
            //int i = axTTDriverMgr1.CloseDriver(TTDeviceMgr.DT_CARDREADER);
            //if (i != 0)
            //{
            //    MyALert.MsgInfo("关闭端口失败 错误码：" + axTTDriverMgr1.GetDeviceStatus(TTDeviceMgr.DT_CARDREADER).ToString());
            //    return;
            //}
            #endregion
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Msgvisable(this.label4, true);

            }
            catch (Exception ex)
            {

                MyMsg.MsgInfo(ex.Message);
                closePort();
                e.Result = "失败";
                return;

            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Equals("失败"))
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }
            //while (!backgroundWorker2.IsBusy)
            //{
            //    Thread.Sleep(10);
            //}
            // m.Close();
            Msgvisable(this.label4, false);
            this.DialogResult = DialogResult.OK;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Thread.Sleep(1000);
                backgroundWorker1.RunWorkerAsync();
                timer1.Stop();
                this.top1.timer1.Stop();
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            m.alerttype = "WaitReadCard";
            if (m.ShowDialog() == DialogResult.OK)
            {
                e.Result = "成功";
            }
            else
            {
                e.Result = "失败";
            }
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result.ToString() == "成功")
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        #region 线程提示区
        private delegate void Demsgvisable(Label lv, bool isvisable);
        //提示信息
        void Msgvisable(Label lv, bool isvisable)
        {
            if (!lv.InvokeRequired)
            {
                lv.Visible = isvisable;
            }
            else
            {
                // 多线程调用时，通过主线程去访问
                Demsgvisable de = Msgvisable;
                this.Invoke(de, lv, isvisable);
            }
        }
        #endregion

        private void txtInputCard_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void lblClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
